package top.raoqq.sky.util;

/**
 * @author 饶奇奇
 * @date 2023/8/4
 */
public class CaptchaUtils {
    public static String getMathResult(String captchaStr) {
        // 去除验证码字符串中的空格和等号
        captchaStr = captchaStr.replace(" ", "").replace("=", "");

        // 解析数学运算表达式
        String[] parts = captchaStr.split("[+\\-*/]");
        int operand1 = Integer.parseInt(parts[0]);
        int operand2 = Integer.parseInt(parts[1]);
        char operator = captchaStr.charAt(parts[0].length());

        // 计算数学运算的结果
        int result;
        switch (operator) {
            case '+':
                result = operand1 + operand2;
                break;
            case '-':
                result = operand1 - operand2;
                break;
            case '*':
                result = operand1 * operand2;
                break;
            case '/':
                result = operand1 / operand2;
                break;
            default:
                throw new IllegalArgumentException("验证码运算表达式非法" + operator);
        }

        return String.valueOf(result);
    }
}
